home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / FILES.C < prev    next >
Text File  |  1993-11-06  |  7KB  |  283 lines

  1. /* Definitions of various files, spool directories, etc
  2.  * especially for a PC (under MS-DOS/DR-DOS)
  3.  */
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #include "files.h"
  9. #include "socket.h"
  10.  
  11. char *EtcRoot        = "/etc";
  12. char *SpoolRoot        = "/spool";                    /* spool 'root' */
  13.  
  14. char *BBSwrkdir        = "/bbs";
  15. char *Alias         = "/alias";                    /* the alias file */
  16. char *Dfile         = "/domain.txt";            /* Domain cache */
  17. char *Fdir             = "/finger";                /* Finger info directory */
  18.  
  19. char *Startup         = "/wnos.rc";                /* Initialization file */
  20. char *Ftpdir          = "/public/upload";            /* */
  21.  
  22. char *Mailqdir         = "/mqueue";                /* Outgoing mail spool */
  23. char *Mailspool     = "/mail";                    /* Incoming mail */
  24. char *Routeqdir     = "/rqueue";                /* queue for router */
  25. char *Rewritefile     = "/rewrite";                /* Address rewrite file */
  26. char *Signature     = "/signatur";                /* Mail signature file directory */
  27. char *News            = "/news";                      /* Top of NeWs file system */
  28. char *Active          = "/active";                /* Active article file */
  29. char *Pointer         = "/pointer";
  30. /* char *NInfo         = "/xinfo";  */
  31. char *Nhelp           = "/help";
  32. char *History         = "/history";
  33. char *Forward         = "/junk";
  34. char *Poll            = "/poll";
  35.  
  36. char *Axroutefile     = "/axroute.dat";            /* AX25 routes */
  37. char *Arproutefile    = "/arproute.dat";            /* ARP entries */
  38. char *Iproutefile     = "/iproute.dat";           /* IP route entries */
  39. #ifdef NETROM
  40. char *Nrroutefile     = "/nrroute.dat";            /* NET/ROM routes */
  41. #endif
  42.  
  43. char Eol[] = "\r\n";
  44. #define    SEPARATOR    "/"
  45.  
  46. static char roott[MAXPATH];
  47.  
  48. /* Concatenate root, separator and arg strings into a malloc'ed output
  49.  * buffer, then remove repeated occurrences of the separator char
  50.  */
  51. static char * near
  52. strcatdup(char *a,char *b)
  53. {
  54.     int index = 0;
  55.  
  56.     char *p1, *p2, *cp, *r,
  57.       *out = mxalloc(strlen(roott) + strlen(SEPARATOR) + strlen(a) + strlen(b) + 2);
  58.  
  59.     while((cp = strchr(a,'\\')) != NULLCHAR) {
  60.         *cp = '/';
  61.     }
  62.     if(b != "") {
  63.         sprintf(out,"%s%s",b,a);
  64.     } else {
  65.         sprintf(out,"%s%s%s",roott,SEPARATOR,a);
  66.     }
  67.     /* Remove any repeated occurrences of the separator char */
  68.     if(out[1] == ':') {
  69.         index = 2;
  70.     }
  71.     p1 = p2 = &out[index];
  72.     r = &roott[index];
  73.  
  74.     while(*p2 != '\0') {
  75.         *p1++ = *p2++;
  76.         while(*p2 == p2[-1] && *p2 == *r) {
  77.             p2++;
  78.         }
  79.     }
  80.     *p1 = '\0';
  81.  
  82.     return out;
  83. }
  84.  
  85. /* Establish a root directory other than the default. Can only be called
  86.  * once, at startup time
  87.  */
  88. void
  89. initroot(char *root)
  90. {
  91.     strcpy(roott,(root != NULLCHAR) ? root : "");
  92.  
  93.     EtcRoot = strcatdup(EtcRoot,"");            /* must be the first! */
  94.     SpoolRoot = strcatdup(SpoolRoot,"");        /* must be the second! */
  95.  
  96.     BBSwrkdir = strcatdup(BBSwrkdir,"");
  97.     Alias = strcatdup(Alias,EtcRoot);
  98.     Dfile = strcatdup(Dfile,EtcRoot);
  99.     Fdir = strcatdup(Fdir,"");
  100.  
  101.     Startup = strcatdup(Startup,EtcRoot);
  102.     Ftpdir = strcatdup(Ftpdir,"");
  103.  
  104.     Mailqdir = strcatdup(Mailqdir,SpoolRoot);
  105.     Mailspool = strcatdup(Mailspool,SpoolRoot);
  106.     Routeqdir = strcatdup(Routeqdir,SpoolRoot);
  107.     Rewritefile = strcatdup(Rewritefile,SpoolRoot);
  108.     Signature = strcatdup(Signature,SpoolRoot);
  109.  
  110.     News = strcatdup(News,SpoolRoot);
  111.     Active = strcatdup(Active,News);
  112.     Pointer = strcatdup(Pointer,News);
  113. /*    NInfo = strcatdup(NInfo,News); */
  114.     Nhelp = strcatdup(Nhelp,News);
  115.     History = strcatdup(History,News);
  116.     Forward = strcatdup(Forward,News);
  117.     Poll = strcatdup(Poll,News);
  118.  
  119.     Axroutefile = strcatdup(Axroutefile,EtcRoot);
  120.     Arproutefile = strcatdup(Arproutefile,EtcRoot);
  121.     Iproutefile = strcatdup(Iproutefile,EtcRoot);
  122. #ifdef NETROM
  123.     Nrroutefile = strcatdup(Nrroutefile,EtcRoot);
  124. #endif
  125. }
  126.  
  127. unsigned int _tmpnum = 0;
  128.  
  129. /*----------------------------------------------------------------------*
  130.  * low-level routine used by tmpnam, fixed to use TEMP env. variable    *
  131.  *----------------------------------------------------------------------*/
  132. static char *pascal
  133. __nmkname(char *tmpname,unsigned tmpnum)
  134. {
  135.     char *tmpdir, *p;
  136.     static char staticname[MAXPATH];
  137.  
  138.     if(tmpname == NULL) {
  139.         tmpname = staticname;
  140.     }
  141.     if((p = getenv("TEMP")) == NULL) {            /* get tempdir name */
  142.         p = "";
  143.     }
  144.     tmpdir = p;
  145.  
  146.     if(*p != '\0') {
  147.         p += strlen(p) - 1;                    /* point to last character */
  148.     }
  149.     sprintf(tmpname,"%s%sTMP%u.TMP",
  150.         tmpdir,
  151.         (*p != '/' && *p != '\\') ? "\\" : "",
  152.         tmpnum);
  153.  
  154.     return tmpname;
  155. }
  156.  
  157. /*-----------------------------------------------------------------------*
  158.  * new tmpnam function.                                                  *
  159.  * calls above routine to generate the name, but                         *
  160.  * is otherwise the same as the tmpnam() in the library.                 *
  161.  *-----------------------------------------------------------------------*/
  162. char *
  163. __tmpnam (char *name,int num)
  164. {
  165.     if(num == 0) {
  166.         do {
  167.             if(_tmpnum == 0xffff) {
  168.                 _tmpnum = 2;
  169.             } else {
  170.                 ++_tmpnum;
  171.             }
  172.             name = __nmkname(name,_tmpnum);
  173.         } while(access(name,0) != -1);
  174.     } else {
  175.       name = __nmkname(name,num);
  176.     }
  177.     return name;
  178. }
  179.  
  180. /*-----------------------------------------------------------------------*
  181.  * tmpfile() to circumvent the FIXUP Overflow error of the Turbo C++     *
  182.  * linker.                               DK5DC                           *
  183.  *-----------------------------------------------------------------------*/
  184. FILE *
  185. Tmpfile(int s,int t)
  186. {
  187.     char maxname[MAXPATH];
  188.     FILE *fp;
  189.  
  190.     __tmpnam(maxname,0);                    /* make the name             */
  191.  
  192.     fp = Fopen(maxname,"w+b",s,t);        /* return FILE *               */
  193.     fp->istemp = _tmpnum;
  194.  
  195.     return fp;
  196. }
  197.  
  198. int
  199. Fclose(FILE *fp)
  200. {
  201.     if(fp->istemp) {
  202.         char maxname[MAXPATH];
  203.         int ret;
  204.  
  205.         __tmpnam(maxname,fp->istemp);
  206.         fp->istemp = 0;
  207.         ret = fclose(fp);
  208.         unlink(maxname);
  209.         return(ret);
  210.     }
  211.     return(fclose(fp));
  212. }
  213.  
  214. /* main file-opening routine
  215.  * options: s = socketnumber, if given an error msg is printed to the socket
  216.  * returncode: NULLFILE if error; filepointer success
  217.  */
  218. FILE *
  219. Fopen(char *name,char *mode,int s,int t)
  220. {
  221.     FILE *fp = NULLFILE;
  222.  
  223.     if(name == NULLCHAR || mode == NULLCHAR || chkbaddoschars(name)) {
  224.         goto err;
  225.     }
  226.     if((fp = fopen(name,mode)) == NULLFILE) {
  227.         if(s) {
  228.             usprintf(s,"503 Fatal error FILE %s\n",name);
  229.         }
  230.         if(t) {
  231. err:
  232.             tprintf("Can't open %s: %s\n",name,sys_errlist[errno]);
  233.         }
  234.     }
  235.     return fp;
  236. }
  237.  
  238. /* create lockfile */
  239. int
  240. mlock(char *dir,char *id)
  241. {
  242.     int fd;
  243.     char lockname[MAXPATH];
  244.  
  245.     sprintf(lockname,"%s/%s.lck",dir,id);
  246.  
  247.     if((fd = open(lockname,O_WRONLY|O_EXCL|O_CREAT,0600)) == -1) {
  248.         return -1;
  249.     }
  250.     close(fd);
  251.     return 0;
  252. }
  253.  
  254. /* remove lockfile */
  255. int
  256. rmlock(char *dir,char *id)
  257. {
  258.     char lockname[MAXPATH];
  259.  
  260.     sprintf(lockname,"%s/%s.lck",dir,id);
  261.  
  262.     return(unlink(lockname));
  263. }
  264.  
  265. int
  266. chkbaddoschars(char *name)
  267. {
  268.     char *cp;
  269.     static char badchars[] = "\"[]|<>+=;,?!";
  270.  
  271.     while((cp = strchr(name,'\\')) != NULLCHAR) {
  272.         *cp = '/';
  273.     }
  274.     /* Check for characters illegal in MS-DOS file names */
  275.     for(cp = badchars; *cp != '\0'; cp++) {
  276.         if(strchr(name,*cp) != NULLCHAR) {
  277.             /* wrong char in filename */
  278.             return 1;
  279.         }
  280.     }
  281.     return 0;
  282. }
  283.